package com.project.web.intfc.utils.appInterface;

import com.project.web.intfc.utils.contst.InterfaceConstant;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.yun.common.utils.appInterface.RESPONSE_CODE_ENUM;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义map属性验证器，通用接口map参数属性验证
 *
 * @author WYY
 * @date 2018.07
 */
public class CustomMapValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return Map.class.isAssignableFrom(clazz);
    }

    /**
     * 通用参数验证
     *
     * @param target 验证对象
     * @param errors 验证错误
     */
    @Override
    public void validate(Object target, Errors errors) {

        // 判断验证对象参数是否为空
        if (target == null) {
            errors.reject(RESPONSE_CODE_ENUM.MISS_PARAMETER.getCode(), RESPONSE_CODE_ENUM.MISS_PARAMETER.getMsg());
        }
        Map map = ((Map) target);

        //判断是否包含待验证的参数
        if (map.containsKey(InterfaceConstant.MOIBLE_INTERFACE_FILTER_NAME)) {
            List<String> filterList = (List) map.get(InterfaceConstant.MOIBLE_INTERFACE_FILTER_NAME);
            for (String filter : filterList) {
                String value = (String) map.get(filter);

                //非空验证
                if (value == null || !StringUtils.hasLength(value.toString())) {
                    errors.reject(RESPONSE_CODE_ENUM.MISS_PARAMETER.getCode(), RESPONSE_CODE_ENUM.MISS_PARAMETER.getMsg());
                    return;
                }

                //邮箱验证
                if (filter.contains(InterfaceConstant.MOBILE_PARAMETER_EMAIL_NAME)) {
                    Pattern emailPattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
                    Matcher m = emailPattern.matcher(value);
                    if (!m.matches()) {
                        errors.reject(RESPONSE_CODE_ENUM.TYPE_MIS_MATCH.getCode(), RESPONSE_CODE_ENUM.TYPE_MIS_MATCH.getMsg());
                        return;
                    }
                }

                //手机号验证
                if (filter.contains(InterfaceConstant.MOBILE_PARAMETER_MOBILE_NAME)) {
                    Pattern mobilePattern = Pattern.compile("^1(3[0-9]|4[57]|5[0-35-9]|7[06-8]|8[0-9])\\d{8}$");
                    Matcher m = mobilePattern.matcher(value);
                    if (!m.matches()) {
                        errors.reject(RESPONSE_CODE_ENUM.TYPE_MIS_MATCH.getCode(), RESPONSE_CODE_ENUM.TYPE_MIS_MATCH.getMsg());
                        return;
                    }
                }

                //日期格式验证
                if (filter.contains(InterfaceConstant.MOBILE_PARAMETER_TIME_NAME) || filter.contains(InterfaceConstant.MOBILE_PARAMETER_DATE_NAME)) {
                    try {
                        Date date = DateUtils.parseDate(value, new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"});
                        if (date == null) {
                            errors.reject(RESPONSE_CODE_ENUM.TYPE_MIS_MATCH.getCode(), RESPONSE_CODE_ENUM.TYPE_MIS_MATCH.getMsg());
                            return;
                        }
                    } catch (ParseException e) {
                        errors.reject(RESPONSE_CODE_ENUM.TYPE_MIS_MATCH.getCode(), RESPONSE_CODE_ENUM.TYPE_MIS_MATCH.getMsg());
                        return;
                    }
                }
            }
        }
    }

}
